Customize the font color and size of labels

Source: Internet
Author: User

Needless to say, directly on the code, very simple, are native, note that the label can not be used. Text, to use. attributedtext!!

#import "ViewController.h"

@interface Viewcontroller () <UITableViewDataSource,UITableViewDelegate>
{
Nsmutablearray *_fontarray;
}

@property (Weak, nonatomic) Iboutlet UITableView *tableview;

@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];
Self.tableView.rowHeight = 80.0f;
_fontarray = [[Nsmutablearray alloc] initwithcapacity:242];
For (NSString * familyname in [Uifont Familynames]) {
NSLog (@ "Font familyname =%@", familyname); * Output font Family section name
For (NSString * fontname in [Uifont Fontnamesforfamilyname:familyname]) {
NSLog (@ "%@", FontName);
[_fontarray Addobject:fontname];
}
}
}

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
{
return _fontarray.count;
}

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
{
static NSString *id = @ "Cell";
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:id];
if (cell = = nil) {
cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:id];
}
NSString * teststr = @ "It has words";
nsmutableattributedstring *str = [[Nsmutableattributedstring alloc] initwithstring:teststr];
[Str addattribute:nsforegroundcolorattributename value:[uicolor Bluecolor] Range:nsmakerange (0,3)];
[Str addattribute:nsforegroundcolorattributename value:[uicolor Redcolor] Range:nsmakerange (3,4)];
[Str addattribute:nsforegroundcolorattributename value:[uicolor Greencolor] Range:nsmakerange (7,4)];
[Str addattribute:nsfontattributename value:[uifont Fontwithname:_fontarray[indexpath.row] size:30] Range: Nsmakerange (5, 6)];
Cell.textLabel.attributedText = str;
Cell.detailTextLabel.text = [nsstring stringwithformat:@ "font name:%@", _fontarray[indexpath.row]];
return cell;
}

@end

Customize the font color and size of labels

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.