我有一個像「這是一個好蘋果」的字符串。在我的UIlabel上顯示。我想用不同的字體來設置單詞「good」。只是看起來像「這是一個好蘋果。」是否可以在一個UIlabel中設置不同的字體?
回答
看看NSAttributedString ...使用OHAttributedLabel畫NSAttributedString因爲的UILabel,UITextView的亙古不變的支持NSAttributedString ...
PS:如果你打算分發iOS6的或更新的應用程序,因爲現在的UILabel支持NSAttributedString,您應該直接使用UILabel而不是OHAttributedLabel,因爲它現在由操作系統本機支持。
它似乎很酷!,thx! – 2012-04-20 08:41:24
我還沒有測試過,但我*認爲*這是過時的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
@MarkAmery我已更新我的其他答案,但忘記更新它。感謝您的通知。 http://stackoverflow.com/questions/4208282/advance-search-implementation-in-iphone/4208329#4208329 – KingofBliss 2013-08-16 12:10:24
這對於UILabel是不可能的。但是你可以使用Core Text。
其他更簡單的方法可能是使用非常小的UIWebView。在這裏你可以使用html命令設置字體。
我很抱歉,但是這是不可能用的UILabel實現,你有兩個選擇:
- 您可以創建一個UIWebView並把HTML格式的文本
- 你可以使自己的UILabel實現與functionallity例如,使用Core Text
而不是使用兩個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
}
我會檢查這個代碼,真的thx :) – 2012-09-20 10:15:53
有設置不同的/多種字體使用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中。
不工作.. [NSDictionary dictionaryWithObject:ArialFont forKey:NSFontAttributeName] ;在這一行中得到錯誤 – 2013-09-06 11:51:00
@RajeshPillai你有沒有在第一行定義字體? UIFont * ArialFont = [UIFont fontWithName:@「arial」size:18.0]; – Akshay 2013-09-08 08:40:37
修改阿克沙伊的答案,並實現了它與斯威夫特:
// 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
沒有理由使用第三方解決方案或子類。
- 1. 是否可以設置字母之間的UILabel距離?
- 2. 爲UILabel設置一些不同字體的問題
- 3. UILabel - 以編程方式在iPhone中設置字體 - 字體
- 4. Javascript:是否可以在2D畫布中爲不同的文字設置不同的字體大小?
- 5. 是否可以在UILabel中設置多種顏色?
- 6. UILabel設置中的字體問題iOS
- 7. 是否可以在同一個表中引用不同的列?
- 8. 是否可以在一個DIV中設置3個背景?
- 9. 是否可以爲文件「passwd」設置不同的位置?
- 10. 是否可以在一個鍵下設置一個NSMutableDictionary鍵?
- 11. 如何在iOS中的UILabel上設置粗體字體?
- 12. SpannableString是否可以設置在一個片段中?
- 13. 是否可以使用&&並在同一行上設置變量?
- 14. 是否可以在window.onPopState中設置e.preventDefault?
- 15. 是否可以在FileStream中設置BufferSize?
- 16. 是否可以爲NSString設置自定義字體和大小?
- 17. 是否可以在PHPMAILER中發送不同的主體內容?
- 18. 是否可以在一個Jelastic應用上設置多個SSL?
- 19. 什麼是迅速的一個基本的字體,我可以使用的UILabel
- 20. 是否可以設置VoiceMailNumber?
- 21. 我是否可以在模型中設置字段的評論?
- 22. 的UILabel使用不同的字體
- 23. UILabel - 設置自定義字體
- 24. 是否可以在Android中設置圖片而不是文本
- 25. 是否可以在Angular 2中設計不同的組件?
- 26. 在IBOutletCollection設置一個UILabel的文本
- 27. 是否可以設置不同的網格視圖不同行的列數?
- 28. android在按鈕中設置2個不同的字體大小
- 29. 是否可以設置另一個進程窗體的MinimumSize屬性? C#
- 30. 是否可以在流暢配置中使用不同的CurrentSessionContexts?
我創建了一個簡單的課程來做到這一點:http://past.is/NDe3 – AncAinu 2014-01-23 08:09:09