iOS,如何绘制文字的动态长度?

标签 ios uiview uiimage uilabel

图片为:http://www.flickr.com/photos/71607441@N07/6641626163/

背景是一个 UIImageView,蓝色部分我想显示为图像的“标题”。 我使用了 UILabel,但文本的长度是动态的。可以是一行,也可以是两行,最多两行。如果文本超过两行,它将被截断。

蓝色部分看起来像“Microsoft Word 中的高亮”,但不是“iOS UILabel.text 中的高亮”

有没有人可以帮助我?

最佳答案

试试这个代码:----

        CGSize maximumSize = CGSizeMake(320, 30);

UILabel *newsLabel = [[UILabel alloc] init];
                    newsLabel.textColor = [UIColor whiteColor];
                    newsLabel.font = [UIFont boldSystemFontOfSize:11];
                    newsLabel.backgroundColor = [UIColor clearColor];
                    newsLabel.lineBreakMode = UILineBreakModeWordWrap;
                    newsLabel.numberOfLines = 0;
                    lineBreakMode:newsLabel.lineBreakMode];
                    CGSize dateStringSize = [@"Text Input" sizeWithFont:newsLabel.font 
                                                                       constrainedToSize:maximumSize 
                                                                           lineBreakMode:newsLabel.lineBreakMode];
                    CGRect dateFrame = CGRectMake(5, 5, 320, dateStringSize.height); //breath can be any desired float value


                    newsLabel.text = @"Text Input";
                    newsLabel.textAlignment = UITextAlignmentCenter; 
                   newsLabel.frame = dateFrame;

关于iOS,如何绘制文字的动态长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8745407/

相关文章:

ios - 在 UIViewController Storyboard 中访问 UIView

ios - UIImage 切片导致崩溃

iPhone UIView 以编程方式替换图像颜色,就像在 Photoshop 或 Illustrator 中一样

ios - ScrollView 大小与 xcode 中的设备屏幕不匹配

ios - 单元格一开始不出现,但如果我向下滚动并返回,则会显示

ios - 在 Objective C 中解密 AES-256-CBC

ios - 在 iOS 中,如果 UILabel 继承自 UIView 那么为什么 UILabel 不能使用 animate 方法?

ios - 在 iOS Swift 3 中将 openGL ES 绘图保存为 PNG 图像文件

ios - 如何将图像裁剪成圆角

ios - 在混合应用程序中从 react native View 导航到 ios View