[英]Bold part of the text in UITextView in Swift using NSMutableAttributedString
[英]NSMutableAttributedString not working: bold the part of NSString
嘗試加粗NSString的開始部分。 使用下面提到的代碼。
-(void)setText {
NSString *strEmail = @"Email: HR_Contact@sre.com";
NSMutableAttributedString *attributedEmail = [[NSMutableAttributedString alloc]initWithString:strEmail];
NSString *boldFontName = [[UIFont fontWithName:_fontMyriadBold size:20] fontName];
NSRange boldedRange = NSMakeRange(0, 5);
[attributedEmail beginEditing];
[attributedEmail addAttribute:NSFontAttributeName
value:boldFontName
range:boldedRange];
[attributedEmail endEditing];
lblEmailAddress.attributedText = attributedEmail;
}
它沒有任何改變。 為什么這樣,問題是沒有得到警告或錯誤。 請指導。 提前致謝。
檢查一下。
NSString * strEmail = @"Email: HR_Contact@sre.com";
NSMutableAttributedString * attributedEmail = [[NSMutableAttributedString alloc] initWithAttributedString:strEmail];
NSRange boldedRange = NSMakeRange(0, 5);
[attributedEmail addAttribute: NSFontAttributeName value:[UIFont fontWithName:_fontMyriadBold size:20] range:boldedRange];
[attributedEmail addAttribute: NSForegroundColorAttributeName value: [*UICOLOR*] range:boldedRange]; // if needed
[lblEmailAddress setAttributedText: attributedEmail];
您在哪里設置此屬性字符串? 也許在-init
之一中? 也許lblEmailAddress
仍為零,您正在嘗試設置它。 如果是這樣,這就是它對您不起作用的原因。 如果我正確理解lblEmailAddress
來自* .xib。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.