我在我的应用程序之一所做的就是创建文本,像这样:
NSString *sectionTitle = @"YOUR TITLE HERE";
CGSize sz = [sectionTitle sizeWithFont:[UIFont boldSystemFontOfSize:20.0f]
constrainedToSize:CGSizeMake(290.0f, 20000.0f)
lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(sz.height, 20.0f);
CGRect sectionFrame = CGRectMake(0.0, 0.0, 320.0, height);
我用290约束宽度给两边的标签房客。然后我用像这样的可拉伸的图像:
并缩放,以适合在标题文本:
UIImage *headerImage = [UIImage imageNamed:@"sectionheaderbackground.png"];
UIImage *stretchableImage = [headerImage stretchableImageWithLeftCapWidth:12 topCapHeight:0];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:sectionFrame];
backgroundImageView.image = stretchableImage;
// Add it to the view for the header
UIView *sectionView = [[UIView alloc] initWithFrame:sectionFrame];
sectionView.alpha = 0.9;
sectionView.backgroundColor = [UIColor clearColor];
[sectionView addSubview:backgroundImageView];
最后,我创建的标签,并添加它作为一个子视图:
CGRect labelFrame = CGRectMake(10.0, 0.0, 290.0, height);
UILabel *sectionLabel = [[UILabel alloc] initWithFrame:labelFrame];
sectionLabel.text = sectionTitle;
sectionLabel.numberOfLines = 0;
sectionLabel.font = [UIFont boldSystemFontOfSize:18.0];
sectionLabel.textColor = [UIColor whiteColor];
sectionLabel.shadowColor = [UIColor grayColor];
sectionLabel.shadowOffset = CGSizeMake(0, 1);
sectionLabel.backgroundColor = [UIColor clearColor];
[sectionView addSubview:sectionLabel];
return sectionView;
作为所有的颜色组件是相同的(247/255),更简单的修改原生是: 'UIColor(白色:0.97,alpha:1)' 其中0.97〜247/255 – 2017-03-15 10:20:31