我新来客观-C .. 我想的UILabels
设置动态大小(width
和height
)。我有一个UILabel
用它。如果一些文本的文本是大,应该正确显示,如果它在亘古不变的契合那一行通过去其他行,如果数据完成,那么只有下一个UILabels
数据应该开始,并且对于下一个UILabel
也是相同的概念。我怎样才能得到它...?将动态大小设置为xcode中的UILabels?
0
A
回答
1
以下是对viewcontroller.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
IBOutlet UITableView *dataTableView;
NSMutableArray *items;
}
@end
继代码为viewcontroller.m文件的代码
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
items = [[NSMutableArray alloc] init];
[items addObject:@"Happiness is having a large, loving, caring, close-knit family in another city.\n\n\t\t-George Burns (1896 - 1996)"];
[items addObject:@"When I am abroad, I always make it a rule never to criticize or attack the government of my own country. I make up for lost time when I come home.\n\n\t\t-Sir Winston Churchill (1874 - 1965)"];
[items addObject:@"After two years in Washington, I often long for the realism and sincerity of Hollywood.\n\n\t\t-Fred Thompson, Speech before the Commonwealth Club of California"];
[items addObject:@"It is a profitable thing, if one is wise, to seem foolish.\n\n\t\t-Aeschylus (525 BC - 456 BC)"];
[items addObject:@"Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions.\n\n\t\t-Dave Barry"];
[items addObject:@"At the worst, a house unkept cannot be so distressing as a life unlived.\n\n\t\t-Dame Rose Macaulay (1881 - 1958)"];
[items addObject:@"It is curious that physical courage should be so common in the world and moral courage so rare.\n\n\t\t-Mark Twain (1835 - 1910)"];
[items addObject:@"The knowledge of the world is only to be acquired in the world, and not in a closet.\n\n\t\t-Lord Chesterfield (1694 - 1773), Letters to His Son, 1746, published 1774"];
[items addObject:@"What lies behind us and what lies before us are tiny matters compared to what lies within us.\n\n\t\t-Ralph Waldo Emerson (1803 - 1882), (attributed)"];
}
#pragma mark -
#pragma mark UITableView Delegaates
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text 1="systemFontOfSize:FONT_SIZE" language="sizeWithFont:[UIFont"][/text] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] ;
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:NSLineBreakByWordWrapping];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
[[cell contentView] addSubview:label];
}
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text 1="systemFontOfSize:FONT_SIZE" language="sizeWithFont:[UIFont"][/text] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[dataTableView deselectRowAtIndexPath:[dataTableView indexPathForSelectedRow] animated:YES];
}
@end
Also Download Source code from here.
愿这帮助了很多。
0
How to set label according to text but i want background color also
使用这个链接,在这里你用“TEXTSIZE”设置您的标签框架和backview
相关问题
- 1. 多动态大小的UILabels不显示
- 2. 如何垂直布置动态大小的UILabels?
- 3. 为Asp:Chart控件设置动态大小
- 4. iphone为UILabels设置了固定的字体大小
- 5. 将设置大小设置为TreeSet
- 6. 将数字从UILabels中划分为小数 - xcode
- 7. 将轮播宽度动态设置为窗口大小
- 8. Xcode:将不同高度的UILabels约束为固定大小的容器
- 9. 具有动态设置大小的UITableView
- 10. 动态设置UICollectionViewCell的大小
- 11. 动态设置ImageView的大小Android
- 12. 用swift动态设置TableViewCell的大小
- 13. 的UITextView与滚动动态调整大小设置为默认
- 14. UIScrollView动态设置页面大小
- 15. c数组:动态设置大小?
- 16. WIA:设置动态页面大小
- 17. 如何动态设置图像大小?
- 18. 如何动态设置div大小?
- 19. 将小部件大小设置为其他大小的一半
- 20. 将UIButton设置为UICollectionViewCell的大小
- 21. 设置图像资源的动态setImageResource设置大小
- 22. 动态设置指针为int给予不正确的大小
- 23. 自动设置UIScrollView内容大小为滚动显示大小
- 24. 如何将scrollview的大小设置为textview的大小?
- 25. 如何将IFRAME的大小设置为容器的大小?
- 26. 动态调整UILabels
- 27. 将TextBlock的大小设置为动态更改宽度的按钮
- 28. 如何将Ext.field.TextArea大小设置为没有滚动条的文本的大小?
- 29. 如何将大小设置为全屏?
- 30. 将字体大小设置为窗口
点击这里给我的答案http://stackoverflow.com/a/12600584/1538079 –
可能重复[如何动态计算UILabel高度?](http://stackoverflow.com/questions/7174007/how-to-calculate-uilabel-height-dynamically) –