Tag: uilabel

如何计算UILabel上的NSString的宽度和高度

我正在一个项目上dynamic地UILabel宽度和高度上的NSString。 我试着用: NSString *text = [messageInfo objectForKey:@"compiled"]; writerNameLabel.numberOfLines = 0; writerNameLabel.textAlignment = UITextAlignmentRight; writerNameLabel.backgroundColor = [UIColor clearColor]; CGSize constraint = CGSizeMake(296,9999); CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; NSLog(@"sizewidth = %f, sizeheight = %f", size.width, size.height); NSLog(@"writerNameLabel.frame.size.width 1 -> %f",writerNameLabel.frame.size.width); [writerNameLabel setFrame:CGRectMake(writerNameLabel.frame.origin.x, writerNameLabel.frame.origin.y, size.width, size.height)]; CGRect labelFram = writerNameLabel.frame; labelFram.origin.x = cell.frame.size.width – writerNameLabel.frame.size.width […]

iPhone的iOS UILabel如何自定义文本颜色的UITableView细节文本标签只?

我正在制作一个界面原型,并正在使用一个故事板来做到这一点。 部分原型涉及将UITableView单元格的详细UILabel设置为某种颜色。 我想避免在故事板中手动重新着色每个标签。 我发现我可以使用: [[UILabel appearanceWhenContainedIn:[UITableViewCell class], nil] setTextColor:[UIColor cyanColor]]; 改变tableview单元格中标签的外观。 有没有办法进一步完善这个代码只适用于详细tableview标签? 目前它改变了UITableViewCell的textLabel和detailTextLabel。 谢谢!

NSLineBreakMode枚举和向后兼容性

我需要我的应用程序来支持iOS 5 +。 由于之前的iOS 6中, UILabel中的UILineBreakMode是UILineBreakModetypes的,并且它是iOS 6+的NSLineBreakModetypes,所以应该最好(或更正确)检查当前运行的iOS版本以确定types被使用? 直接做[[UIDevice currentDevice] systemVersion]是否正确,还是有更好的方法? 谢谢!

UILabeldynamic字体大小继续打破

我有CollectionViewCell只有UILabel(与单元格相同的边界)。 我正在获取作为CollectionView数据源的fontNames数组: func printFonts() { let fontFamilyNames = UIFont.familyNames() for familyName in fontFamilyNames { let names = UIFont.fontNamesForFamilyName(familyName as! String) fontNames.append(familyName as! String) } } 我试图在UILabel中的一行显示这个字体名称,它在一些情况下,但在一些不工作,我不知道为什么。 这是我如何“调整” cellForItemAtIndexPath的字体: cell.lbl_font.adjustsFontSizeToFitWidth = true cell.lbl_font.numberOfLines = 1 cell.lbl_font.minimumScaleFactor = 0.1 cell.lbl_font.baselineAdjustment = .AlignCenters cell.lbl_font.textAlignment = NSTextAlignment.Center 以及我通过storyBoard修改了这个属性: 结果: 更新: 我如何应用文本到单元格。 其中lbl_font是单元格内部的cellForItemAtIndexPath let cell : textCell = collectionView.dequeueReusableCellWithReuseIdentifier("text_cell", forIndexPath: […]

TTTAttributedLabel“阅读更多>”可能有几个属性的尾部截断?

TTTAttributedLabel通过truncationTokenString以及truncationTokenStringAttributes支持一个自定义的截断string。 但是,我想要进一步并在截断string上设置几个string属性,包括不同的字体和颜色。 这是我想要实现的: 最后的箭头可以使用字体图标来实现,所以我想到了下面的string: @"… Read More >" 'HORIZONTAL ELLIPSIS' (U+2026) + > character from a font Read More + > character from a font 。 不幸的是, TTTAttributedLabel不允许我设置各种属性的范围。 有没有人有一个很好的解决scheme,或将不得不手动这样做,基本上计算string,因为它可以在两行,包括@"… Read More >"string。 谢谢!

iOS UILabel autoshrink,所以单词不会被截断为两行

我试图让UILabel缩小,以便单词不会被截断到下一行。 不只是文本截断在文本区域的末尾。 如果我有一个50×100的盒子,我想在25.0pt的盒子里放一些“美式”的东西,那么我得到: 50px ——- |Ameri- | |can | |Beauty | 100px | | ——- 在这种情况下,文本缩小似乎没有做任何事情,因为它仍然适合UILabel框架。 如果文本真的很长,像“Willie Wonka的巧克力工厂”,但是我不想截词。 这是这种情况下的理想输出: 50px ——– [American| |Beauty | 100px | | | | | | ——– 任何build议将超级appreicated! 编辑:解决scheme 以下是答案中的build议。 这很好用! – (CGFloat) calculateFromUILabel:(UILabel *)label { NSString *stringToMeasure = label.text; NSLog(@"FontSizeMeasurement.calculateFromUILabel() %@", stringToMeasure); NSRange range = NSMakeRange(0, 1); NSAttributedString *attributedString […]

具有多种字体颜色的UILabel文本

有没有办法让UILabel的textColor属性是两个不同的UIColors ? 基本上我试图使UILabel text property的第一个字符是greenColor ,其余的是blackColor 。 我想避免使用两个不同的UILabels因为我可能想要改变绿色字符文本中的位置。

sizeToFitfunction奇怪

每当发生后端数据库更改时,我都会执行一段代码。 基本上我有一个父视图内的标签。 该标签由多条状态消息中的一条更新,每条消息在不同的行上由新行(\ n)结尾。 每个状态消息只需要在一行上,不要超过。 我遇到的问题是,当视图第一次重新加载时,一切正常。 但是,当我在屏幕上,并且在后台发生更改时,偶尔会有多行显示状态消息。 尽pipesizeToFit将标签制作得和父视图一样高,但它在宽度方面的performance却差强人意。 这是它将分割多行文本,而不是只查看换行符。 我能做什么? self.messageLabel.text = message; //lets get the status message. [self.messageLabel sizeToFit]; [self.messageLabel setNeedsDisplay]; CGRect frame = [self.messageView frame]; frame.size.height = self.messageLabel.bounds.size.height; self.messageView.frame = frame; self.messageLabel.center = CGPointMake(CGRectGetMidX(self.messageView.bounds), CGRectGetMidY(self.messageView.bounds)-(self.messageView.cornerRadius/4)); self.messageLabel.backgroundColor = [UIColor orangeColor]; [self.messageView setNeedsDisplay];

如何调整文字(字体)以适应UISegmentedControl的UISegment?

有没有什么办法可以缩小UISegmentedControl单个部分的字体大小? 已经尝试了很多东西, [[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] adjustsFontSizeToFitWidth]; [[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setMinimumScaleFactor:0.5]; 和 NSArray *arr = segment.subviews; // segment is UISegmentedControl object for (int i = 0; i < arr.count; i++) { UIView *aSegment = [arr objectAtIndex:i]; for (UILabel *label in aSegment.subviews) { if ([label isKindOfClass:[UILabel class]]) { UILabel *myLabel = (UILabel *)label; [myLabel setNumberOfLines:0]; […]

dynamicUILabel截断文本

我使用这个答案中提供的代码来创build一个dynamic标签,它大部分工作。 但是,每当标签文本超过94个字符的长度,它会被截断,省略号被添加。 还有一个更奇怪的事情是,如果我添加更多的字符到string,他们显示,但最后2行仍然被截断。 例如。 string: this is a very very long string with lots of words to test the dynamic bubble sizing one two three. 如下所示: this is a very very long string with lots of words to test the dynamic bubble sizing one tw… 但是,当我在标签中再次使用相同的句子来加倍string时,它会显示更多的te文本,但仍会截断它。 例如。 string: this is a very very long string […]