您好,登錄后才能下訂單哦!
最后發(fā)現(xiàn)需要6.0以后,因為nsfontattributename之類的是6.0以后的api
長久以來,我以為富文本是一種在ios中使用特別麻煩的事情,但是不經(jīng)意的研究發(fā)現(xiàn),其實并沒有那么難!
以下的代碼實現(xiàn)了uilabel中放置富文本。
NSMutableAttributedString *richTask = [[NSMutableAttributedString alloc] initWithString:task]; NSDictionary *urgentAttributes = @{NSFontAttributeName : [UIFont fontWithName:@"Courier" size:24], NSStrokeWidthAttributeName : @3.0,NSStrokeColorAttributeName:[UIColor greenColor]}; [richTask setAttributes:urgentAttributes range:urgentRange];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *identifier = nil; NSString *task = [self.tasks objectAtIndex:indexPath.row]; NSRange urgentRange = [task rangeOfString:@"URGENT"]; if (urgentRange.location == NSNotFound) { identifier = @"plainCell"; } else { identifier = @"attentionCell"; } UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; // Configure the cell... UILabel *cellLabel = (UILabel *)[cell viewWithTag:1]; NSMutableAttributedString *richTask = [[NSMutableAttributedString alloc] initWithString:task]; NSDictionary *urgentAttributes = @{NSFontAttributeName : [UIFont fontWithName:@"Courier" size:24], NSStrokeWidthAttributeName : @3.0,NSStrokeColorAttributeName:[UIColor greenColor]}; [richTask setAttributes:urgentAttributes range:urgentRange]; if (urgentRange.location != NSNotFound) { NSRange otherPartRange = NSMakeRange(urgentRange.length+urgentRange.location, task.length-urgentRange.length); NSDictionary* otherPartAttributes = @{NSFontAttributeName : [UIFont fontWithName:@"Courier" size:12], NSStrokeWidthAttributeName : @0,NSStrokeColorAttributeName:[UIColor redColor]}; [richTask setAttributes:otherPartAttributes range:otherPartRange]; } cellLabel.attributedText = richTask; return cell; }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。