2017-08-07 98 views
1

權目前,我試圖創建一個應用程序中的消息功能。調整的UILabel對齊到屏幕

我想對齊的UILabel從用戶發送到屏幕右側的消息。隨着文本寬度內容的增加,我希望它可以伸展到屏幕的左側,然後一旦達到最大寬度就換行。

正如你可以看到我有文字換行正確,但我不能讓文字正確坐在屏幕上。

我對這個問題的解決辦法是,以編程方式調整的UILabel的寬度,但我一直無法得到它從固定大小的我在自定義傳出消息細胞的XIB文件更改

莫非有人請告知如何做到這一點?

編輯:

我畫箱過的UILabel的電流範圍,以顯示它正在做什麼 Box drawn over UILabel bounds

這是文本移動和的UILabel勢必重繪解釋什麼,我試圖做。 Box and text moved to show what I'm trying to do.

應用 Screen shot of messaging feature in application

+0

你應用了文本對齊嗎? https://developer.apple.com/documentation/uikit/uilabel/1620541-textalignment – paulvs

+0

@paulvs我做了一些編輯,以更好地解釋我遇到的問題。我有正確的文本對齊,是我的錯誤。問題是UILabel的大小我不確定如何更改 – Airagale

+0

因此,您想縮小標籤以適合其內容?您是否使用約束或框架來定位標籤? – paulvs

回答

0

這裏的截圖,你應該計算基於文本長度的標籤的寬度。 嘗試使用此API根據文本輸入獲取標籤的大小。 CGSize yourLabelSize = [yourLabel.text sizeWithAttributes:@ {NSFontAttributeName:[UIFont fontWithName:yourLabel.font size:yourLabel.fontSize]}];}};

我不低於你的情況。但你可以試一試。

在Xcode中創建一個標籤,使用自動佈局來設置填充(大多數4點在尾隨&靈活的領先空間> 4分)。請確保將行數設置爲「0」並且文本的右側對齊。不要爲寬度設置固定的大小這裏

+0

不幸的是,我正在尋找的解決方案是不對文本進行右對齊。 – Airagale

0

enter image description here

您可以通過測量文本的長度,並呼籲大小以適合標籤上達到預期的效果。然後,將標籤的框架(或需要時的周圍超級視圖)偏移到屏幕的左側或右側。

這裏有一個快速的方法(可能是最好聲明變量maxWidth和填充其他地方)。請注意,'w'是屏幕寬度的變量。

-(UIView *)bubbleWithText:(NSString *)text atOffset:(float)yOff isLeftAligned:(bool)leftAligned { 

    float maxWidth = 200.0f; 
    float intraCellPadding = 5.0f; 

    UILabel * label = [UILabel new]; 
    label.frame = CGRectMake(intraCellPadding, intraCellPadding, maxWidth, 0); 
    label.textColor = (leftAligned) ? [UIColor blackColor] : [UIColor whiteColor]; 
    label.text = text; 
    label.numberOfLines = 0; 
    [label sizeToFit]; 

    float width = label.frame.size.width; 
    float height = label.frame.size.height; 
    float xOff = (leftAligned) ? intraCellPadding : w-3*intraCellPadding-width; 

    UIView * bubble = [UIView new]; 
    bubble.frame = CGRectMake(xOff, yOff, width + 2 * intraCellPadding, height + 2 * intraCellPadding); 
    bubble.backgroundColor = (leftAligned) ? [UIColor greenColor] : [UIColor blueColor]; 
    bubble.layer.cornerRadius = intraCellPadding; 
    [bubble addSubview:label]; 

    return bubble; 

} 

而且你會調用上面是這樣的:

float interCellPadding = 10.0f; 
float yOff = 20.0f; 
bool previousWasLeft = false; 

NSArray * texts = @[@"Bacon ipsum dolor amet kevin swine ham hock, leberkas porchetta salami bacon picanha shoulder pig strip steak sirloin.", 
        @"Short loin hamburger meatloaf jowl, sirloin swine pork belly tail t-bone venison.", 
        @"Flank meatloaf prosciutto ball tip strip steak rump.", 
        @"Short piggy.", 
        @"Tri-tip ribeye drumstick andouille leberkas bacon pork chop meatball pork spare ribs chicken.", 
        @"Strip steak filet mignon tri-tip sausage, cow frankfurter bacon ribeye cupim burgdoggen shank pork belly fatback ham hock."]; 

for (NSString * text in texts){ 

    //random assignment left/right 
    //by creating a random number, either one or zero 
    //this doubles up as a boolean 
    bool alignLeft = arc4random_uniform(2); 

    //here we increment the offset 
    //depending on whether the previous bubble 
    //was on the same side or not, giving a 
    //bit more space for alternating bubbles. 
    yOff += (previousWasLeft != alignLeft) ? interCellPadding : interCellPadding/2.0f; 
    previousWasLeft = alignLeft; 

    //create the bubble and add 
    UIView * bubble = [self bubbleWithText:text atOffset:yOff isLeftAligned:alignLeft]; 
    [self.view addSubview:bubble]; //or scrollView whatever 

    //incrmeent the offset 
    yOff += bubble.frame.size.height; 

} 

這是快速和骯髒的,但展示瞭如何實現你所需要的。對於很多細胞的(這可能是你想要的),如果你要自定義路線,你必須留意從內存中釋放他們。調整UICollectionView單元格並簡單地對齊內容可能會更容易。