我在我的應用程序之一所做的就是創建文本,像這樣:
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