开发者

how to change the color of text in string when I use stringWithFormat function?(iphone)

I am using this in a project,

lblDetail.text =[NSString stringWithFormat:@"%@:%@",lblName.text,lblDetail.text];

in this lblName and lblDetail are both different strings,

Now I want that when I show lblDetail开发者_JAVA技巧 in my view, The text of lblName should be shown in green color.

is this possible ?

I want to do like this

http://imageshack.us/photo/my-images/269/comment2c.png/


If you want to assign different colors (or other style attributes like font and size) to different parts of your string, you might be able to use NSAttributedString and some extensions that support its drawing, e.g. OHAttributedLabel. Unfortunately you won't get any support out of the box in the iOS SDK.


If you need a UILabel that can display some Rich-Text, like a text with the first part in blue and the other in red, you can use my OHAttributedLabel class, which is a subclass of UILabel that uses CoreText to draw NSAttributedStrings. (iOS 3.2+)

It is very simple to use and also provide a category for NSAttribtedString that adds some useful methods, like easily change the style of a range of text without the need to know the CoreText attributes constants.

[EDIT] Sorry I didn't realized that Thomas Wanner already mentioned my class in his answer


There is a property of a UILabel that let's you set the text color:

myLabel.textColor = [UIColor greenColor];

Is lblDetail always visible in your view, or only after a condition is met?


To Give the Font and Color and BackGroundColor to your UILable

lblOfflineCount.textColor=[UIColor grayColor];
lblOfflineCount.font=[UIFont boldSystemFontOfSize:14.0];
[lblOfflineCount setBackgroundColor:[UIColor clearColor]];

for Swift

lblOfflineCount.textColor = UIColor.grayColor()
lblOfflineCount.font = UIFont.boldSystemFontOfSize(14.0)
lblOfflineCount.backgroundColor = UIColor.clearColor()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜