簡體   English   中英

使用iOS 7 SDK構建時,UITextView會包裝文本

[英]UITextView wraps text when built with iOS 7 SDK

我在UIScrollView中有一個UITextView ,它在從xcode 4.x構建的iOS 6上完美運行,但是現在使用xcode 5構建它甚至在iOS 6上也無法正常工作。

問題是文本包含屏幕寬度,即使UITextViewUIScrollView具有較大的寬度。 我使用此代碼來計算UITextView的新寬度和高度,即使textview向左/向右滾動,文本也會被包裹,好像寬度只是屏幕的寬度。

謝謝

self.responceTextView.text = [NSString stringWithFormat:@"%@%@",_responceTextView.text,responce];
[self textViewDidChange:self.responceTextView];

- (void)textViewDidChange:(UITextView *)textView
{
    // Recalculate size of text field
    CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
    CGSize reqSize = [textView.text sizeWithFont:[UIFont fontWithName:@"Courier" size:12] constrainedToSize:maxSize lineBreakMode:NSLineBreakByClipping];

    self.responceTextView.frame = CGRectMake(0, 0, reqSize.width+16, reqSize.height+16);

    // Resize scroll view if needs to be smaller so text stays at top of screen
    CGFloat maxScrollHeight = maxScrollViewSize.size.height;
    if (self.responceTextView.frame.size.height < maxScrollHeight) {
        self.responceScrollView.frame = CGRectMake(self.responceScrollView.frame.origin.x, self.responceScrollView.frame.origin.y, self.responceScrollView.frame.size.width, self.responceTextView.frame.size.height);
    } else {
        self.responceScrollView.frame = maxScrollViewSize;
    }

    // Set content size
    self.responceScrollView.contentSize = CGSizeMake(self.responceTextView.frame.size.width, self.responceTextView.frame.size.height);

    [self scrollToCursor];
}

編輯----

好的,所以似乎sizeWithFont在iOS 7中已棄用。奇怪的是我沒有得到編譯器警告。 它仍然沒有意義,它在iOS 6上不起作用(或者在使用iOS 7 SDK構建時是否完全刪除?)

我嘗試了這兩種替代方案,但從所有方面獲得完全相同的大小。

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIFont fontWithName:@"Courier" size:12], NSFontAttributeName,
                                nil];
CGRect rect = [textView.text boundingRectWithSize:maxSize options:NSLineBreakByClipping | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil];

返回: {{0, 0}, {439.27148, 168}}

CGSize rect2 = [textView.text sizeWithAttributes:attributes];

返回: {439.27148, 168}

以上原文返回{439.27148, 168}

他們都應該回歸更廣闊的視野。

編輯2 ----從上面看,返回的幀是正確的(439寬)但是它仍然是文本視圖中包含的文字。

嘗試使用:

[textView.text boundingRectWithSize:CGSizeMake(txtFrame.size.width, CGFLOAT_MAX)
                           options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                           attributes:[NSDictionary dictionaryWithObjectsAndKeys:textView.font,NSFontAttributeName, nil] context:nil];

字符串測量似乎非常多。 對於我所做的測試,這是唯一能夠提供合適尺寸的選項組合。

我在iOS7中成功使用以下代碼(它是具有最小和最大高度的UITextField 。當文本的高度變大然后MAX_HEIGHT_MESSAGE_TEXTBOX ,滾動條出現在UITextField )。

const float MAX_HEIGHT_MESSAGE_TEXTBOX = 80;
const float MIN_HEIGHT_MESSAGE_TEXTBOX = 30;


- (void)setFrameToTextSize:(CGRect)txtFrame textView:(UITextView *)textView
{

    if(txtFrame.size.height > MAX_HEIGHT_MESSAGE_TEXTBOX)
    {
        //OK, the new frame is to large. Let's use scroll
        txtFrame.size.height = MAX_HEIGHT_MESSAGE_TEXTBOX;
        textView.scrollEnabled = YES;
        [textView scrollRangeToVisible:NSMakeRange([textView.text length], 0)];
    }
    else
    {
        if (textView.frame.size.height < MIN_HEIGHT_MESSAGE_TEXTBOX) {
             //OK, the new frame is to small. Let's set minimum size
            txtFrame.size.height = MIN_HEIGHT_MESSAGE_TEXTBOX;
        }
        //no need for scroll
        textView.scrollEnabled = NO;
    }
    //set the frame
    textView.frame = txtFrame;
}

- (void)setframeToTextSize:(UITextView *)textView animated:(BOOL)animated
{
    //get current height
    CGRect txtFrame = textView.frame;

    //calculate height needed with selected font. Note the options.
    //append a new line to make space for the cursor after user hit the return key
    txtFrame.size.height =[[NSString stringWithFormat:@"%@\n ",textView.text]
                           boundingRectWithSize:CGSizeMake(txtFrame.size.width, CGFLOAT_MAX)
                           options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                           attributes:[NSDictionary dictionaryWithObjectsAndKeys:textView.font,NSFontAttributeName, nil] context:nil].size.height;

    if (animated) {
        //set the new frame, animated for a more nice transition
        [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseOut |UIViewAnimationOptionAllowAnimatedContent animations:^{
            [self setFrameToTextSize:txtFrame textView:textView];
        } completion:nil];
    }
    else
    {
        [self setFrameToTextSize:txtFrame textView:textView];
    }
}

- (void)textViewDidChange:(UITextView *)textView
{
    [self setframeToTextSize:textView animated:YES];
}

編輯

當字符串測量正確時,您可能需要更改UITextView的textContainer上的lineBreakMode NSTextContainerNSTextContainer中的一個新類,包含有關如何布局文本的信息):

textView.textContainer.lineBreakMode = NSLineBreakByCharWrapping; // default is NSLineBreakByWordWrapping 

祝好運!

方法sizeWithFont:(UIFont *)font constrainedToSize ..."已在iOS 7中棄用。

它會正常運作。

在iOS 7中查看它的備用

NSString實例方法

-(CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:

(NSDictionary *)attributes context:(NSStringDrawingContext *)context

檢查這個答案。 在iOS 7中替換已棄用的sizeWithFont:

我想你想要計算內容大小,這樣你也可以增加滾動視圖的內容大小。 如果是,請使用此鏈接, https://stackoverflow.com/a/19152955/1023083

回答“謝謝,但這似乎沒有解決我的問題。我使用靈活的寬度和高度。似乎我得到正確的尺寸返回,因為textview滾動到兩個方向足夠,但文本本身仍在被文字包裹。“

我通過[textView sizeToFit]修復了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM