Objective c 对齐UITextView的最后一行
我正在做一个像书一样的项目。其中有多个页面上的段落列表,您可以滚动浏览。我为每个页面创建单独的Objective c 对齐UITextView的最后一行,objective-c,swift,uitextview,justify,Objective C,Swift,Uitextview,Justify,我正在做一个像书一样的项目。其中有多个页面上的段落列表,您可以滚动浏览。我为每个页面创建单独的UITextView,并将其设置为创建类似书本的体验。不幸的是,UITextView的最后一行是不正确的(因为我对每个页面都使用了新的UITextView)。因此实际上,UITextView的行为是正确的。但是,当我的段落在下一页继续时,我想通过证明最后一行来表达这一点。我正在使用UITextView为用户提供选择和复制部分文本的选项 我在UITextView上添加了一个类别,以执行最后一行所需的计算
UITextView
,并将其设置为创建类似书本的体验。不幸的是,UITextView
的最后一行是不正确的(因为我对每个页面都使用了新的UITextView
)。因此实际上,UITextView
的行为是正确的。但是,当我的段落在下一页继续时,我想通过证明最后一行来表达这一点。我正在使用UITextView
为用户提供选择和复制部分文本的选项
我在UITextView上添加了一个类别,以执行最后一行所需的计算。这可以是单独的.h/.m文件,也可以放在需要使用它的.m文件的顶部。请注意,这对attributedText和textContainer属性进行操作。即使仅在UITextView上设置了text属性,也会设置attributedText属性(从iOS7开始)。textContainer属性定义UITextView将在其中呈现其文本的区域的大小,但如果使用约束来布局UITextView,则可能需要稍微不同
@interface UITextView (LastLineRange)
- (NSRange) lastLineRange;
@end
@implementation UITextView (LastLineRange)
- (NSRange)lastLineRange
{
// calculate the bounds for the text rendered in the UITextView
CGSize textSize = [self.attributedText boundingRectWithSize:self.textContainer.size
options:NSStringDrawingUsesLineFragmentOrigin
context:nil].size;
// we care about the last line, so get the start and end points along the bottom of the bounds
UITextPosition* start = [self characterRangeAtPoint:CGPointMake(0, textSize.height)].start;
UITextPosition* end = [self characterRangeAtPoint:CGPointMake(textSize.width, textSize.height)].end;
// turn the text positions into indices we can make a range with
NSInteger startLoc = [self offsetFromPosition:self.beginningOfDocument
toPosition:start];
// this should not be 'endOfDocument', but the end that we calculated above,
// since this represents the end of the visible text
NSInteger endLoc = [self offsetFromPosition:start
toPosition:end];
return NSMakeRange(startLoc, endLoc);
}
@end
这就是我如何使用该类别获取最后一行并对其应用右对齐段落样式的方法。我试着添加一些评论,以明确正在发生的事情。我刚刚粗略地画出了实际的UITextView,所以它没有您的示例那么漂亮,但应该能给您提供想法
- (void)viewDidLoad
{
[super viewDidLoad];
UIFont* font = [UIFont systemFontOfSize:20.0];
// used for the main, left-aligned text
NSMutableParagraphStyle* pStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
pStyle.lineBreakMode = NSLineBreakByWordWrapping;
pStyle.alignment = NSTextAlignmentLeft;
// used for the last line, which will be right-aligned
NSMutableParagraphStyle* lastStyle = [pStyle mutableCopy];
lastStyle.alignment = NSTextAlignmentRight;
CGRect pageFrame = CGRectMake(self.view.frame.size.width / 4.0,
50.0,
self.view.frame.size.width / 2.0,
self.view.frame.size.height / 2.0);
UITextView* page = [[UITextView alloc] initWithFrame:pageFrame
textContainer:nil];
page.scrollEnabled = false;
page.editable = false;
// attributes for main text
NSDictionary* lAlignedAttrs = @{
NSFontAttributeName : font,
NSParagraphStyleAttributeName : pStyle
};
// attributes for the last line of text
NSDictionary* rAlignedAttrs = @{
NSFontAttributeName : font,
NSParagraphStyleAttributeName : lastStyle,
// only added this to highlight the last line changes
NSForegroundColorAttributeName : [UIColor redColor]
};
// needs to be mutable, so we can set attributes at certain ranges
NSMutableAttributedString* text1 = [[NSMutableAttributedString alloc] initWithString:text
attributes:lAlignedAttrs];
// now set the attributed text as content for the UITextView, since
// we will use the category for finding the last line
page.attributedText = text1;
// here, we ensure that the last line will be considered a new paragraph
// so the paragraph style changes we make will take effect
NSInteger insertionIndex = page.lastLineRange.location;
if ( [text1.string characterAtIndex:insertionIndex] != '\n' )
{
[text1 insertAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]
atIndex:insertionIndex];
}
// apply right-aligned paragraph style over the range of the last line
[text1 setAttributes:rAlignedAttrs
range:page.lastLineRange];
// we have altered the attributed text, so need to set the new text
// as the content for the UITextView
page.attributedText = text1;
[self.view addSubview:page];
}
@end
以下是我在这方面取得的成就:
添加到目前为止您尝试过的代码。@PGDev我找不到任何只影响最后一行的代码。不知道怎么处理。我只能通过设置textAlignment属性来影响其他行。您好,我刚刚编辑了我的问题以包含预期内容。希望这能让事情变得更清楚。@svojtko我编辑了这篇文章,展示了我为你工作的结果。如果我误解了你的意思,请告诉我。