简体   繁体   中英

Can I put a UIButton or UITextField or UITextView between the texts in swift

Can I put a UIButton or UITextField or UITextView between the texts in Swift

examples:

  1. "What is your **[UITextField]** about downloading free music files from the Internet? **[UIButton]**"

  2. "What is your **[UITextField]** about
    downloading free
    music files from the
    Internet? **[UIButton]** "

Like "example2", the layout may change depending on the situation.

Not a particularly easy task. You can use attributed text to stylize a word and make it "tappable" (like a hyperlink on a webpage), but that wouldn't solve the issue of embedding an editable text field in the string.

One approach would be to embed "placeholder" text in your string, then find the bounding box for that word and overlay a textfield or button. You'd have to be sure to account for things like word wrap, and it would take some experimentation to get the widths right.

So, you might set the text of your label to:

What is your UITextFieldGoesHere about downloading free music files from the Internet? UIButtonHere

Then use code to find the bounding box / rect of UITextFieldGoesHere and position a text field on top of that, so it covers the word and looks like it is inline. Same thing with the UIButtonHere .

If your button might be simply OK , and you don't want it wide with left-right padding, change that placeholder in your string to something like OKB ... just make sure it is unique so you can find it.

Lots of examples out there for finding the bounding box / rect of a word in a label... use Google (or your favorite search engine) to search for:

uilabel find bounding box of specific word

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM