![](/img/trans.png)
[英]how to set custom UILabel text on UITableView prototype cell
[英]How to set attributed text to UILabel which defined in custom UITableView cell
將屬性文本設置為在uitableview單元格中定義的uilable有點麻煩。 我在uitableview的cellForRowAtIndexPath委托方法中設置屬性文本,但不影響輸出。 我只是希望uilabel的文本將顯示為粗體文本。 我的代碼如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MessagesTable *msgobject=[messages objectAtIndex:indexPath.row];
static NSString* cellIdentifier = @"messagingCellText";
TextTableViewCell *ccell = (TextTableViewCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (ccell == nil)
{
ccell = [[TextTableViewCell alloc] initMessagingCellWithReuseIdentifier:cellIdentifier MessageType:msgobject.messageMediaType];
cell2.backgroundColor=[UIColor clearColor];
cell2.userInteractionEnabled=YES;
cell2.layer.shouldRasterize = YES;
cell2.layer.rasterizationScale = [UIScreen mainScreen].scale;
}
NSString *newString = "This is Bold, This is regular";
NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc]initWithString:newString];
NSRange rangeOfSubstring = [newString rangeOfString:@"This is Bold"];
[attrStr addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"GandhiSans-Bold" size:16]
range:rangeOfSubstring];
ccell.messageLabel.attributedText = attrStr;
return ccell;
}
我的initMessagingCellWithReuseIdentifier:方法如下:
-(id)initMessagingCellWithReuseIdentifier:(NSString*)reuseIdentifier MessageType:(NSString *)msgTypePara{
if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {
/*Message-Label*/
self.messageLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.messageLabel.backgroundColor = [UIColor clearColor];
//self.messageLabel.font = [UIFont systemFontOfSize:messageTextSize];
self.messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.messageLabel.numberOfLines = 0;
self.messageLabel.textColor=[UIColor blackColor];
self.messageLabel.textAlignment=NSTextAlignmentLeft;
//self.messageLabel.font=[UIFont preferredFontForTextStyle:UIFontTextStyleBody];
//self.messageLabel.font = [UIFont fontWithName:@"GandhiSans-regular" size:messageTextSize];
self.messageLabel.userInteractionEnabled=YES;
[self.messageLabel sizeToFit];
[self addSubview: self.messageLabel];
[self.contentView addSubview: self.messageView];
return self;
}
試試看,請用您的特定代碼替換它,並檢查是否發生任何更改
NSString *newString = @"This is Bold, This is regular";
NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc]initWithString:newString];
NSRange rangeOfSubstring = [newString rangeOfString:@"This is Bold"];
[attrStr addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:16]
range:rangeOfSubstring];
[attrStr addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:rangeOfSubstring];
嘗試這個:
NSMutableAttributedString *MylabelAttributes = [[NSMutableAttributedString alloc] initWithString:@"my test"];
[MylabelAttributes addAttribute:NSForegroundColorAttributeName value:[UIColor yourcolor] range:NSMakeRange(0,MylabelAttributes.length)];
[MylabelAttributes addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:19.0]
range:NSMakeRange(0,MylabelAttributes.length)];
self.yourlabel.attributedText=MylabelAttributes;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.