根据标签文本在ios中动态定位按钮

时间:2015-07-24 11:28:53

标签: ios objective-c iphone xcode6

我在标签下面贴了一个标签和两个按钮。 我想根据标签中的文字动态地向上和向下移动按钮。 As in the image I have a label and three line data within it.and two Button below it

in this image I have data in single line in label then button should be shift upward accordingly

3 个答案:

答案 0 :(得分:1)

CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];   

//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;

根据标签的高度设置按钮的框架

答案 1 :(得分:0)

使用Autolayout代替硬编码。

Set Accept button left edge constraint to label left edge constraint.
Set Decline button right edge constraint to label right edge constraint. 
Add vertical space between buttons and label.

Take label height constraint reference and set it's constant at runtime which is equal to label height.

答案 2 :(得分:0)

使用:

对于接受按钮:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class grabber {
     public static void main(String[] args) throws Exception {
            String url = "https://orderup.com/restaurants/bella-pizza-r3834/delivery";
            Document document = Jsoup.connect(url).get();
            Elements restname = document.select("h1.urbana");
            System.out.println("restname: " + restname.text());
            Elements restaddressdiv = document.select("address.desktop-address");
            Elements restauranthours = document.select("div.restaurant-hours-region");
            Elements restauranthoursa = restauranthours.select("div.restaurant-hours-region");
            Elements restauranthoursregion = restauranthoursa.select("dt");
            System.out.println("restauranthosssurs: " + restauranthoursregion.size());
            for (Element resthours : restauranthoursregion) {
                System.out.println("restauranthours: " + resthours.text());
            }
            Elements h3 = document.select("div.menu-category");
            Elements h3tag = h3.select("h3");
            for(Element e : h3tag)
            {
                 System.out.println("Category: " + e.text());  

                 if (e.nextElementSibling().select("p").size() == 1) {
                     Elements itemtitlep =e.nextElementSibling().select("p");
                     Elements itemtitle = e.nextElementSibling().select("span.item-title");
                     System.out.println(itemtitle.size());
                        int itemtitleCount = itemtitle.size();
                        System.out.println("ifffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff statement");
                        for(Element itema : itemtitle)
                        {
                            System.out.println("Items: " + itema.text());
                            Elements itemtitleprice = itema.nextElementSibling().select(".item-price");
                            Elements itemtitledes = itema.getElementsByTag("p");
                            for(Element itempricea : itemtitleprice)
                            {
                                System.out.println("price: " + itempricea.text());
                            }
                            for(Element itemdesc : itemtitledes)
                            {
                                System.out.println("itemdesc: " + itemdesc.text());
                            }
                        }
                } else {
                    Elements itemtitle = e.nextElementSibling().select("span.item-title");
                    int itemtitleCount = itemtitle.size();
                    System.out.println(itemtitleCount);
                    System.out.println("elssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss statement");
                    for(Element itema : itemtitle)
                    {
                        System.out.println("Items: " + itema.text());
                        Elements itemtitleprice = itema.nextElementSibling().select(".item-price");
                        Elements itemtitledes = itema.getElementsByTag("p");
                        for(Element itempricea : itemtitleprice)
                        {
                            System.out.println("price: " + itempricea.text());
                        }
                        for(Element itemdesc : itemtitledes)
                        {
                            System.out.println("itemdesc: " + itemdesc.text());

                        }
                    }
                }
            }
        }
}

对于拒绝按钮:

btnAccept!.frame = CGRectMake(CGRectGetMinX(txtView!.frame), CGRectGetMaxY(txtView!.frame) + 20, btnAccept!.frame.size.width, btnAccept!.frame.size.height)

变量btnAccept是接受按钮

变量btnDecline是拒绝按钮

变量txtView是textview