如何为可能是多行字符串的文本加下划线?我发现有些人建议使用UIWebView,但对于文本渲染而言,它显然太重了.
我的想法是弄清楚每一行中每个字符串的起点和长度.并在其下划一条线.
我遇到了如何计算字符串的长度和起点的问题.有人可以帮我这个吗?
我尝试使用-[UILabel textRectForBounds:limitedToNumberOfLines:],这应该是文本的绘图边界矩形吧?然后我必须进行对齐?当中心对齐且右对齐时,如何获得每条线的起点?
我是新来的,所以提前谢谢你.
这是我的代码:
CGSize s = [string sizeWithFont:[UIFont systemFontOfSize:20]
constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40, CGFLOAT_MAX) // - 40 For cell padding
lineBreakMode:UILineBreakModeWordWrap];
Run Code Online (Sandbox Code Playgroud)
我收到一条警告,在iOS 6中不推荐使用UILinebBreakModeWordWrap.
在iOS7中,sizeWithFont不推荐使用,因此我正在使用boundingRectWithSize(返回CGRect值).我的代码:
UIFont *fontText = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16];
// you can use your font.
CGSize maximumLabelSize = CGSizeMake(310, 9999);
CGRect textRect = [myString boundingRectWithSize:maximumLabelSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:fontText}
context:nil];
expectedLabelSize = CGSizeMake(textRect.size.width, textRect.size.height);
Run Code Online (Sandbox Code Playgroud)
在textRect,我的尺寸大于我maximumLabelSize,尺寸与使用时不同sizeWithFont.我该如何解决这个问题?
如何UILabel以编程方式在Swift 中将a的文本设置为粗体?我的代码到目前为止:
var label = UILabel(frame:theFrame)
label.text = "Foo"
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用从Adobe Font Collection Pro Package获得的Helvetica Neue Condensed字体.不幸的是,当我在一个内部使用它时,似乎绘制不正确UILabel.
行高似乎是正确计算的(我认为),但是当显示字体时,它会与边界框的最顶部对齐.我调用[myLabel sizeToFit]并仅调整宽度以产生此屏幕捕获:

我对字体的粗体和常规版本都有同样的问题.我能够从OSX中取出一个版本的Helvetica Neue Bold并将其放在我的设备上并显示正常(上图中的绿色背景).
字体文件或我的代码导致它以这种方式绘制可能有什么问题?
我试图UIButton在其titleLabel中创建一个包含两行文本的文本.这是我正在使用的代码:
UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
[titleButton setTitle:@"This text is very long and should get truncated at the end of the second line" forState:UIControlStateNormal];
titleButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
titleButton.titleLabel.numberOfLines = 2;
[self addSubview:titleButton];
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,文本只出现在一行上.似乎实现多行文本的唯一方法UIButton.titleLabel是设置numberOfLines=0和使用UILineBreakModeWordWrap.但这并不能保证文本正好是两行.
UILabel但是,使用plain 可以工作:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
titleLabel.text = @"This text is very long and should get truncated at the end of …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置UILabel的字体大小.无论我放什么价值,文字大小似乎都没有改变.这是我正在使用的代码.
[self setTitleLabel:[[UILabel alloc] initWithFrame:CGRectMake(320.0,0.0,428.0,50.0)]];
[[self contentView] addSubview:[self titleLabel]];
UIColor *titlebg = [UIColor clearColor];
[[self titleLabel] setBackgroundColor:titlebg];
[[self titleLabel] setTextColor:[UIColor blackColor]];
[[self titleLabel] setFont:[UIFont fontWithName:@"System" size:36]];
Run Code Online (Sandbox Code Playgroud) 我是iPhone和Mac编程的新手(以前为Windows开发过),我有一个问题:
我如何动画text的属性UILabel两个数字之间,例如从5至80在渐出风格?有可能CoreAnimation吗?我一直在Google上搜索一个小时,但我找不到解决问题的方法.我想要的是:为一个简单的游戏动画用户的钱.当它从50到100或没有动画的那样时它看起来不太好.
任何人都知道如何做到这一点?
谢谢!
我需要绘制一个UILabel.因此,我将UILabel子类化并实现如下:
@implementation UIStrikedLabel
- (void)drawTextInRect:(CGRect)rect{
[super drawTextInRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextFillRect(context,CGRectMake(0,rect.size.height/2,rect.size.width,1));
}
@end
Run Code Online (Sandbox Code Playgroud)
会发生的事情是,UILabel的线条与整个标签一样长,但文本可以更短.有没有办法确定文本的长度(以像素为单位),以便可以适当地绘制线条?
我也对任何其他解决方案持开放态度,如果已知:)
最好的,Erik
如何UILabel在Swift中强调一下?我搜索了Objective-C但却无法让它们在Swift中工作.