标签首行宽度不同iOS
Different width of the first line of the label iOS
我想设置标签第一行的宽度,使其小于其余行。
如图所示 - 由于日期原因,需要更短一些。
我该怎么做?
您必须为此目的使用 NSAttributedString。
NSMutableAttributedString *myAttributedString = [[NSMutableAttributedString alloc] initWithString:@"This is my text bla bla bla bla bla!"];
[myAttributedString addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:14.0]
range:NSMakeRange(startingpoint, length)];
通过这种方式,您可以指定第一行(静态长度假设为 20 个字符)的字体大小为 14,其余部分的字体大小更大。
模拟类似的函数为字符串的其余部分创建另一个 attributedString。
现在合并这 2 个 attributedString 以形成一个 attributedString。和写
myLabel.attributedText = myTotalAttributedText
或者你可以通过这个 Core Text 教程,通过它你可以获得相同的预期效果 Link
假设您的目标是 iOS7 或更高版本,您可以使用 TextKit 引入的非常方便的排除路径。
NSTextContainer
(可通过 UITextView
的 textContainer
property) provides an exclusionPaths
属性 访问,您可以将其设置为一个或多个代表区域的 UIBezierPath
文本应在何处流动。
因此,首先您需要确定您不想在其中看到任何文本的矩形(在您的情况下,我猜是时间标签 + 一些填充)。请注意,我们需要在文本视图的坐标系中使用此矩形,因此您可能需要进行转换,例如:
CGRect timeLabelRect = [self.timeLabel convertRect:self.timeLabel.bounds toView:self.textView];
CGRect exclusionRect = CGRectInset(timeLabelRect, -10.f, -10.f); // add padding
一旦我们有了那个 rect,只需从它创建一个路径并将其设置为文本视图文本容器上的(唯一)排除路径即可:
UIBezierPath *path = [UIBezierPath bezierPathWithRect:exclusionRect];
self.textView.textContainer.exclusionPaths = @[path];
就是这样!
Swift 中的完整示例(因为我 copy/pasted 它来自 Playground):
// just a plain container view
var container = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 400))
// the text view
var textView = UITextView(frame: CGRect(x: 0, y: 100, width: 300, height: 300))
textView.text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
container.addSubview(textView)
// a box (representing the time label in your case)
var blueBox = UIView(frame: CGRect(x: 250, y: 100, width: 50, height: 50))
blueBox.backgroundColor = .blueColor()
container.addSubview(blueBox)
此时它看起来像这样:
现在,让我们简单地添加排除路径:
// configure exclusion path
let blueBoxRect = textView.convertRect(blueBox.bounds, fromView: blueBox)
let path = UIBezierPath(rect: blueBoxRect)
textView.textContainer.exclusionPaths = [path]
添加排除路径后是这样的:
UILabel *labelDate = [[UILabel alloc]initWithFrame:CGRectMake(_textView1.frame.size.width-100, 0, 100, 30)];
labelDate.text = @"4:32 PM";
[_textView1 addSubview:labelDate];//just added a sample label
在第一行放置'\n'的代码
int firstlineWidth = _textView1.frame.size.width - labelDate.frame.size.width;
int eachCharWidthSum = 0;
NSMutableString *stringToShow = [[NSMutableString alloc]init];
stringToShow = [_textView1.text mutableCopy];
int previousSpaceCharIndex = 0;
int initialPreviousSpaceCharIndex = 0;
BOOL isNeedNextSpace = NO;
for (int i =0; i<_textView1.text.length; i++)
{
unichar charf = [_textView1.text characterAtIndex:i];
NSString *eachString = [NSString stringWithFormat:@"%c",charf];
CGSize stringSize = [eachString sizeWithFont:_textView1.font];
eachCharWidthSum += stringSize.width;
if ([eachString isEqualToString:@" "])
{
previousSpaceCharIndex = i;
}
int differance = i - previousSpaceCharIndex;
if (firstlineWidth<eachCharWidthSum&&differance<10)
{
[stringToShow insertString:@"\n" atIndex:previousSpaceCharIndex];
break;
}
else if (firstlineWidth<eachCharWidthSum&&differance>10&&isNeedNextSpace==NO)
{
isNeedNextSpace = YES;
initialPreviousSpaceCharIndex = previousSpaceCharIndex;
}
else if (isNeedNextSpace&&initialPreviousSpaceCharIndex!=previousSpaceCharIndex)
{
/*
found new space char
*/
[stringToShow insertString:@"\n" atIndex:previousSpaceCharIndex];
break;
}
}
_textView1.text = stringToShow;
我想设置标签第一行的宽度,使其小于其余行。
如图所示 - 由于日期原因,需要更短一些。 我该怎么做?
您必须为此目的使用 NSAttributedString。
NSMutableAttributedString *myAttributedString = [[NSMutableAttributedString alloc] initWithString:@"This is my text bla bla bla bla bla!"];
[myAttributedString addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:14.0]
range:NSMakeRange(startingpoint, length)];
通过这种方式,您可以指定第一行(静态长度假设为 20 个字符)的字体大小为 14,其余部分的字体大小更大。
模拟类似的函数为字符串的其余部分创建另一个 attributedString。
现在合并这 2 个 attributedString 以形成一个 attributedString。和写
myLabel.attributedText = myTotalAttributedText
或者你可以通过这个 Core Text 教程,通过它你可以获得相同的预期效果 Link
假设您的目标是 iOS7 或更高版本,您可以使用 TextKit 引入的非常方便的排除路径。
NSTextContainer
(可通过 UITextView
的 textContainer
property) provides an exclusionPaths
属性 访问,您可以将其设置为一个或多个代表区域的 UIBezierPath
文本应在何处流动。
因此,首先您需要确定您不想在其中看到任何文本的矩形(在您的情况下,我猜是时间标签 + 一些填充)。请注意,我们需要在文本视图的坐标系中使用此矩形,因此您可能需要进行转换,例如:
CGRect timeLabelRect = [self.timeLabel convertRect:self.timeLabel.bounds toView:self.textView];
CGRect exclusionRect = CGRectInset(timeLabelRect, -10.f, -10.f); // add padding
一旦我们有了那个 rect,只需从它创建一个路径并将其设置为文本视图文本容器上的(唯一)排除路径即可:
UIBezierPath *path = [UIBezierPath bezierPathWithRect:exclusionRect];
self.textView.textContainer.exclusionPaths = @[path];
就是这样!
Swift 中的完整示例(因为我 copy/pasted 它来自 Playground):
// just a plain container view
var container = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 400))
// the text view
var textView = UITextView(frame: CGRect(x: 0, y: 100, width: 300, height: 300))
textView.text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
container.addSubview(textView)
// a box (representing the time label in your case)
var blueBox = UIView(frame: CGRect(x: 250, y: 100, width: 50, height: 50))
blueBox.backgroundColor = .blueColor()
container.addSubview(blueBox)
此时它看起来像这样:
现在,让我们简单地添加排除路径:
// configure exclusion path
let blueBoxRect = textView.convertRect(blueBox.bounds, fromView: blueBox)
let path = UIBezierPath(rect: blueBoxRect)
textView.textContainer.exclusionPaths = [path]
添加排除路径后是这样的:
UILabel *labelDate = [[UILabel alloc]initWithFrame:CGRectMake(_textView1.frame.size.width-100, 0, 100, 30)];
labelDate.text = @"4:32 PM";
[_textView1 addSubview:labelDate];//just added a sample label
在第一行放置'\n'的代码
int firstlineWidth = _textView1.frame.size.width - labelDate.frame.size.width;
int eachCharWidthSum = 0;
NSMutableString *stringToShow = [[NSMutableString alloc]init];
stringToShow = [_textView1.text mutableCopy];
int previousSpaceCharIndex = 0;
int initialPreviousSpaceCharIndex = 0;
BOOL isNeedNextSpace = NO;
for (int i =0; i<_textView1.text.length; i++)
{
unichar charf = [_textView1.text characterAtIndex:i];
NSString *eachString = [NSString stringWithFormat:@"%c",charf];
CGSize stringSize = [eachString sizeWithFont:_textView1.font];
eachCharWidthSum += stringSize.width;
if ([eachString isEqualToString:@" "])
{
previousSpaceCharIndex = i;
}
int differance = i - previousSpaceCharIndex;
if (firstlineWidth<eachCharWidthSum&&differance<10)
{
[stringToShow insertString:@"\n" atIndex:previousSpaceCharIndex];
break;
}
else if (firstlineWidth<eachCharWidthSum&&differance>10&&isNeedNextSpace==NO)
{
isNeedNextSpace = YES;
initialPreviousSpaceCharIndex = previousSpaceCharIndex;
}
else if (isNeedNextSpace&&initialPreviousSpaceCharIndex!=previousSpaceCharIndex)
{
/*
found new space char
*/
[stringToShow insertString:@"\n" atIndex:previousSpaceCharIndex];
break;
}
}
_textView1.text = stringToShow;