给定字体的字符串长度以适合UITextView [英] String length with given font to fit UITextView

查看:111
本文介绍了给定字体的字符串长度以适合UITextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将用户输入的大量多行 UITextView 移动到更小的(但仍然是多行的) UITextView *。如果用户输入的文本比在较小视图中显示的文本多,我想截断文本,使其适合所有(截断的)文本可见。 ( UITextView 也不能滚动。)



最好的方法是什么? / p>

我可以使用循环,每次用字符缩短字符串,然后使用 NSString sizeWithFont:constrainedToSize:lineBreakMode:找出这个较短的字符串需要的高度,然后将其与我在较小的 UITextView ,结束循环,当字符串将适合 - 但这似乎很慢和尴尬。必须有更好的方法。



我想告诉目的地 UITextView 截断其displayText成员因为它显示在屏幕上,但我没有能够找到一种方法来做到这一点。



*关于此的更多内容,请参阅下面的评论:



。我根据用户选择的照片更改视图的布局。如果是横向照片,则字幕会较小 - 在照片底部只有一行。如果她选择了肖像照片,那么我可以使用大量的空间在照片侧面的字幕,因此字幕会更大。



如果用户更改她的照片取向从纵向到横向,然后我想截断文本,然后让她编辑它,使它有意义。

解决方案

我写了下面的递归方法和公共API来做到这一点。丑陋的软件因子是此问题的主题

  #define kFudgeFactor 15.0 
#define kMaxFieldHeight 9999.0

/ /递归方法由主API调用
- (NSString *)sizeStringToFit :( NSString *)aString min:(int)aMin max:(int)aMax
{
if((aMax- aMin)<= 1)
{
NSString * subString = [aString substringToIndex:aMin];
return subString;
}

int mean =(aMin + aMax)/ 2;
NSString * subString = [aString substringToIndex:mean];

CGSize tallerSize = CGSizeMake(self.frame.size.width-kFudgeFactor,kMaxFieldHeight);
CGSize stringSize = [subString sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];

if(stringSize.height< = self.frame.size.height)
return [self sizeStringToFit:aString min:mean max:aMax]; // too small
else
return [self sizeStringToFit:aString min:aMin max:mean]; // too big
}

- (NSString *)sizeStringToFit :(NSString *)aString
{

CGSize tallerSize = CGSizeMake(self.frame.size.width-kFudgeFactor,kMaxFieldHeight);
CGSize stringSize = [aString sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];

//如果合适,只返回
if(stringSize.height< self.frame.size.height)
return aString;

//太大 - 调用递归方法来调整大小
NSString * smallerString = [self sizeStringToFit:aString min:0 max:[aString length]];
return smallString;
}


I need to move text that the user has entered into a large multi-line UITextView into a smaller (but still multi-line) UITextView*. If the user has entered more text than will display in the smaller view, I want to truncate the text so that it fits with all the (truncated) text visible. (Neither the large UITextView nor the smaller one should scroll.)

What's the best way to do this?

I can use a loop, shortening the string by a character each time, and then use NSString's sizeWithFont: constrainedToSize: lineBreakMode: to find out the height this shorter string would need, and then compare that against the height I have available in my smaller UITextView, ending the loop when the string will fit - but that seems slow and awkward. There must be a better way.

I'd like to just tell the destination UITextView to truncate its displayText member as it displays it on screen, but I've not been able to find a way to do that.

*More context on this, from a comment I made below:

I've got a landscape app. I change the layout of the view depending on the photo the user chooses. If it's a landscape photo, the caption is smaller - just a line at the bottom of the photo. If she chooses a portrait photo, then there's plenty of space I can use for the caption at the side of the photo, so the caption is bigger.

If the user changes her photo orientation from portrait to landscape, then I want to truncate the text and then allow her to edit it so that it makes sense. I could just zap it, but I'd prefer to preserve it to minimize her typing.

解决方案

I wrote the following recursive method and public API to do this properly. The ugly fudge factor is the subject of this question.

#define kFudgeFactor 15.0
#define kMaxFieldHeight 9999.0

// recursive method called by the main API
-(NSString*) sizeStringToFit:(NSString*)aString min:(int)aMin max:(int)aMax
{
if ((aMax-aMin) <= 1)
    {
    NSString* subString = [aString substringToIndex:aMin];
    return subString;
    }

int mean = (aMin + aMax)/2; 
NSString* subString = [aString substringToIndex:mean];

CGSize tallerSize = CGSizeMake(self.frame.size.width-kFudgeFactor,kMaxFieldHeight);
CGSize stringSize = [subString sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];

if (stringSize.height <= self.frame.size.height)
    return [self sizeStringToFit:aString min:mean max:aMax]; // too small
else    
        return [self sizeStringToFit:aString min:aMin max:mean];// too big
}

-(NSString*)sizeStringToFit:(NSString*)aString
{

CGSize tallerSize = CGSizeMake(self.frame.size.width-kFudgeFactor,kMaxFieldHeight);
CGSize stringSize = [aString sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];

// if it fits, just return
if (stringSize.height < self.frame.size.height)
    return aString; 

// too big - call the recursive method to size it       
NSString* smallerString = [self sizeStringToFit:aString min:0 max:[aString length]];
return smallerString;   
}

这篇关于给定字体的字符串长度以适合UITextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆