【发布时间】:2014-04-30 08:20:45
【问题描述】:
我想让标签动态化(当文本大小增加时,高度和宽度会发生变化),在 iOS7 中可以吗?发布完整的教程或任何帖子。 提前致谢。
【问题讨论】:
标签: objective-c
我想让标签动态化(当文本大小增加时,高度和宽度会发生变化),在 iOS7 中可以吗?发布完整的教程或任何帖子。 提前致谢。
【问题讨论】:
标签: objective-c
是的。可以这样做:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
...
int fontSize = 13;
int height = ROW_HEIGHT_SCALE;
if(height==5){
fontSize = 10;
}else if(height>5 && height <100){
fontSize = 12;
}else if(height>100 && height <300){
fontSize = 16;
}else if(height>300){
fontSize = 18;
}
[cell.textLabel setFont:[UIFont fontWithName: @"Arial" size: fontSize]];
...
return cell;
}
【讨论】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier =@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
}
UILabel *itemNameLabel = (UILabel *)[cell viewWithTag:1];
itemNameLabel.text = @"Your text";
CGSize labelSize = [self WidthOfCellWithIngredientLine:itemNameLabel];
itemNameLabel.frame = CGRectMake(10, 10, 250, labelSize.height);
}
- (CGSize)WidthOfCellWithIngredientLine:(UILabel *)stringLabel
{
stringLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:15.0f];
CGSize maximumLabelSize = CGSizeMake(310, 9999);
CGSize expectedSize = [stringLabel sizeThatFits:maximumLabelSize];
return expectedSize;
}
请注意:根据标签高度也设置tableCell高度。
在stringLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:15.0f];
设置您在标签上设置的字体名称和字体大小
【讨论】: