2012-04-20 83 views
6

我有一个像“这是一个好苹果”的字符串。在我的UIlabel上显示。我想用不同的字体来设置单词“good”。只是看起来像“这是一个苹果。”是否可以在一个UIlabel中设置不同的字体?

+0

我创建了一个简单的课程来做到这一点:http://past.is/NDe3 – AncAinu 2014-01-23 08:09:09

回答

5

看看NSAttributedString ...使用OHAttributedLabel画NSAttributedString因为的UILabel,UITextView的亘古不变的支持NSAttributedString ...

PS:如果你打算分发iOS6的或更新的应用程序,因为现在的UILabel支持NSAttributedString,您应该直接使用UILabel而不是OHAttributedLabel,因为它现在由操作系统本机支持。

+0

它似乎很酷!,thx! – 2012-04-20 08:41:24

+1

我还没有测试过,但我*认为*这是过时的iOS 6.0。最初的'UILabel'现在有一个'属性文字'属性 - 参见http://developer.apple.com/library/ios/documentation/uikit/reference/UILabel_Class/Reference/UILabel。html#// apple_ref/occ/instp/UILabel/attributedText – 2013-08-16 10:56:50

+0

@MarkAmery我已更新我的其他答案,但忘记更新它。感谢您的通知。 http://stackoverflow.com/questions/4208282/advance-search-implementation-in-iphone/4208329#4208329 – KingofBliss 2013-08-16 12:10:24

1

这对于UILabel是不可能的。但是你可以使用Core Text

其他更简单的方法可能是使用非常小的UIWebView。在这里你可以使用html命令设置字体。

1

我很抱歉,但是这是不可能用的UILabel实现,你有两个选择:

  • 您可以创建一个UIWebView并把HTML格式的文本
  • 你可以使自己的UILabel实现与functionallity例如,使用Core Text
1

而不是使用两个UILabels,您可以使用一个带有多种文本字体样式和颜色的标签。这里有一个例子:

UILabel *customLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 25)]; 
customLbl.backgroundColor = [UIColor yellowColor]; 
[self createTwoTextStyleInSingleUILabel:customLbl];// custom method calling 
[self.view addSubview:customLbl]; 

自定义方法是这样的: 之前应用这种方法,在你的项目中添加QuartzCore框架(需要CALayers)和CoreText框架(所需属性串)。

#import <QuartzCore/QuartzCore.h> 
    #import <CoreText/CoreText.h> 

- (void)createTwoTextStyleInSingleUILabel: (UILabel *) myLabel{ 
NSString *firstText = NSLocalizedString(@"First text:", nil); 
NSString *secondText = [NSString stringWithFormat:@"%@ %@",firstText,@"Second text"]; 
CATextLayer *myLabelTextLayer; 
/* Create the text layer on demand */ 
if (!myLabelTextLayer) { 
    myLabelTextLayer = [[CATextLayer alloc] init]; 
    myLabelTextLayer.backgroundColor = [UIColor clearColor].CGColor; 
    myLabelTextLayer.wrapped = NO; 
    CALayer *layer = myLabel.layer; //assign layer to your UILabel 
    myLabelTextLayer.frame = CGRectMake((layer.bounds.size.width-180)/2 + 10, (layer.bounds.size.height-30)/2 + 10, 180, 30); 
    myLabelTextLayer.contentsScale = [[UIScreen mainScreen] scale]; 
    myLabelTextLayer.alignmentMode = kCAAlignmentCenter; 
    [layer addSublayer:myLabelTextLayer]; 
} 
/* Create the attributes (for the attributed string) */ 
// customizing first string 
CGFloat fontSize = 16; 
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize]; 
CTFontRef ctBoldFont = CTFontCreateWithName((__bridge CFStringRef)boldFont.fontName, boldFont.pointSize, NULL); 
CGColorRef cgColor = [UIColor redColor].CGColor; 
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
          (__bridge id)ctBoldFont, (id)kCTFontAttributeName, 
          cgColor, (id)kCTForegroundColorAttributeName, nil]; 
CFRelease(ctBoldFont); 
// customizing second string 
UIFont *font = [UIFont systemFontOfSize:16]; 
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL); 
CGColorRef cgSubColor = [UIColor blackColor].CGColor; 
NSDictionary *subAttributes = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)ctFont, (id)kCTFontAttributeName,cgSubColor, (id)kCTForegroundColorAttributeName, nil]; 
CFRelease(ctFont); 
/* Create the attributed string (text + attributes) */ 
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:secondText attributes:attributes]; 
float lengthOfSecondString = 12.0; // length of second string including blank space inbetween text, space in front , space after text.. Be careful, your app may crash here if length is beyond the second text length (lengthOfSecondString = text length + blank spaces) 
[attrStr addAttributes:subAttributes range:NSMakeRange(firstText.length, lengthOfSecondString)]; 
// you can add another subattribute in the similar way as above , if you want change the third textstring style 
/* Set the attributes string in the text layer :) */ 
myLabelTextLayer.string = attrStr; 
myLabelTextLayer.opacity = 1.0; //to remove blurr effect 

} 
+0

我会检查这个代码,真的thx :) – 2012-09-20 10:15:53

4

有设置不同的/多种字体使用NSMutableAttributedString上标签&其它性能的方法。 FOLL是我的代码:

UIFont *ArialFont = [UIFont fontWithName:@"arial" size:18.0]; 
NSDictionary *arialdict = [NSDictionary dictionaryWithObject: ArialFont forKey:NSFontAttributeName];  
NSMutableAttributedString *AattrString = [[NSMutableAttributedString alloc] initWithString:title attributes: arialdict]; 

UIFont *VerdanaFont = [UIFont fontWithName:@"verdana" size:12.0]; 
NSDictionary *veradnadict = [NSDictionary dictionaryWithObject:VerdanaFont forKey:NSFontAttributeName]; 
NSMutableAttributedString *VattrString = [[NSMutableAttributedString alloc]initWithString: newsDate attributes:veradnadict];  
[VattrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:(NSMakeRange(0, 15))]; 

[AattrString appendAttributedString:VattrString]; 


lblText.attributedText = AattrString; 

注意lblText是的UILabel,插座,因为文件的所有者。
人们可以继续追加许多NSMutableAttributedString他想..

还要注意,我已经添加了宋体& Arial字体在我的项目&添加一个相同的plist中。

+0

不工作.. [NSDictionary dictionaryWithObject:ArialFont forKey:NSFontAttributeName] ;在这一行中得到错误 – 2013-09-06 11:51:00

+0

@RajeshPillai你有没有在第一行定义字体? UIFont * ArialFont = [UIFont fontWithName:@“arial”size:18.0]; – Akshay 2013-09-08 08:40:37

0

修改阿克沙伊的答案,并实现了它与斯威夫特:

// First part with font size 10 
let firstPartInfo = [NSFontAttributeName: UIFont.systemFontOfSize(10.0)] 
var attributedString = NSMutableAttributedString(string: "First Part", attributes: firstPartInfo) 

// Second part with font size 20 
let secondPartInfo = [NSFontAttributeName: UIFont.systemFontOfSize(20.0)] 
attributedString.appendAttributedString(
    NSAttributedString(string: "Second Part", attributes: secondPartInfo)) 

// Lastly set the attributed text 
label.attributedText = attributedString 

没有理由使用第三方解决方案或子类。

相关问题