ios - Swift how to add background color just around text in a label?

Ios - Swift how to add background color just around text in a label?

To add a background color just around the text in a UILabel in Swift, you can achieve this effect by using NSAttributedString with a background color attribute. Here's a step-by-step guide to implement it:

Using NSAttributedString

  1. Create a UILabel:

    First, create a UILabel instance in your view controller or storyboard:

    let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.text = "Highlighted Text" label.textAlignment = .center label.font = UIFont.systemFont(ofSize: 18) self.view.addSubview(label) 
  2. Apply Background Color Around Text:

    Use NSAttributedString to apply a background color around the text:

    // Define the text and the background color let text = "Highlighted Text" let attributedString = NSMutableAttributedString(string: text) let range = NSRange(location: 0, length: attributedString.length) // Set background color around the text attributedString.addAttribute(.backgroundColor, value: UIColor.yellow, range: range) // Set the attributed text to the label label.attributedText = attributedString 
    • Replace UIColor.yellow with the background color you want.
    • Adjust range to specify which part of the text should have the background color. In this example, it covers the entire text.
  3. Layout Constraints (if not using storyboard):

    If you're not using a storyboard, add layout constraints to position and size the label:

    NSLayoutConstraint.activate([ label.centerXAnchor.constraint(equalTo: view.centerXAnchor), label.centerYAnchor.constraint(equalTo: view.centerYAnchor) ]) 

Example Implementation

Here's how your view controller code might look with the above steps combined:

import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.text = "Highlighted Text" label.textAlignment = .center label.font = UIFont.systemFont(ofSize: 18) self.view.addSubview(label) // Apply background color around text let text = "Highlighted Text" let attributedString = NSMutableAttributedString(string: text) let range = NSRange(location: 0, length: attributedString.length) attributedString.addAttribute(.backgroundColor, value: UIColor.yellow, range: range) label.attributedText = attributedString // Add layout constraints NSLayoutConstraint.activate([ label.centerXAnchor.constraint(equalTo: view.centerXAnchor), label.centerYAnchor.constraint(equalTo: view.centerYAnchor) ]) } } 

Notes:

  • NSAttributedString: Allows you to customize the appearance of text by applying attributes like font, color, and background color.
  • UILabel: Displays styled text and supports attributed strings for rich text formatting.
  • Adjustments: Modify font, text alignment, and other properties as needed to fit your application's design requirements.

By following these steps, you can effectively add a background color just around the text in a UILabel using NSAttributedString in Swift. This approach gives you flexibility in styling text within UILabels beyond basic properties. Adjust the attributes and constraints according to your specific UI design and layout needs.

Examples

  1. "Swift UILabel background color just around text"

    • Description: How to apply a background color specifically around the text inside a UILabel in Swift.
    • Code Implement:
      let label = UILabel() label.text = "Hello World" label.backgroundColor = UIColor.yellow.withAlphaComponent(0.5) label.layer.cornerRadius = 5 // Optional: Adjust the corner radius as needed label.layer.masksToBounds = true 
  2. "iOS Swift highlight text in UILabel with background color"

    • Description: Highlighting text within a UILabel using a background color in Swift.
    • Code Implement:
      let label = UILabel() label.text = "Highlighted Text" label.backgroundColor = UIColor.green.withAlphaComponent(0.3) label.layer.cornerRadius = 8 // Optional: Adjust the corner radius for rounded corners label.layer.masksToBounds = true 
  3. "Swift UILabel background color behind text only"

    • Description: Applying a background color behind text only in a UILabel using Swift.
    • Code Implement:
      let label = UILabel() label.text = "Background Behind Text" label.backgroundColor = UIColor.blue.withAlphaComponent(0.5) label.layer.cornerRadius = 10 // Optional: Customize corner radius label.layer.masksToBounds = true 
  4. "iOS Swift UILabel text highlight background"

    • Description: How to create a highlighted background behind text in a UILabel using Swift.
    • Code Implement:
      let label = UILabel() label.text = "Highlighted Background" label.backgroundColor = UIColor.orange.withAlphaComponent(0.4) label.layer.cornerRadius = 6 // Optional: Set corner radius for rounded edges label.layer.masksToBounds = true 
  5. "Swift UILabel colored background around text"

    • Description: Adding a colored background around text specifically in a UILabel using Swift.
    • Code Implement:
      let label = UILabel() label.text = "Colored Background" label.backgroundColor = UIColor.red.withAlphaComponent(0.3) label.layer.cornerRadius = 12 // Optional: Adjust corner radius for rounded appearance label.layer.masksToBounds = true 
  6. "iOS Swift label background color text area"

    • Description: Setting a background color specifically in the text area of a UILabel in Swift.
    • Code Implement:
      let label = UILabel() label.text = "Text Area Background Color" label.backgroundColor = UIColor.purple.withAlphaComponent(0.5) label.layer.cornerRadius = 4 // Optional: Customize corner radius for rounded corners label.layer.masksToBounds = true 
  7. "Swift UILabel highlight background just text"

    • Description: Highlighting the background behind text only within a UILabel using Swift.
    • Code Implement:
      let label = UILabel() label.text = "Highlight Text Background" label.backgroundColor = UIColor.cyan.withAlphaComponent(0.4) label.layer.cornerRadius = 8 // Optional: Adjust corner radius for rounded edges label.layer.masksToBounds = true 
  8. "iOS Swift UILabel background color text only"

    • Description: Applying a background color behind the text only within a UILabel in Swift.
    • Code Implement:
      let label = UILabel() label.text = "Text Only Background Color" label.backgroundColor = UIColor.gray.withAlphaComponent(0.3) label.layer.cornerRadius = 6 // Optional: Customize corner radius for rounded appearance label.layer.masksToBounds = true 
  9. "Swift UILabel background color around text"

    • Description: How to add a background color around the text content of a UILabel using Swift.
    • Code Implement:
      let label = UILabel() label.text = "Background Around Text" label.backgroundColor = UIColor.magenta.withAlphaComponent(0.5) label.layer.cornerRadius = 10 // Optional: Adjust corner radius for rounded edges label.layer.masksToBounds = true 
  10. "iOS Swift UILabel text highlight with background"

    • Description: Implementing text highlighting with a background color in a UILabel using Swift.
    • Code Implement:
      let label = UILabel() label.text = "Text Highlighted Background" label.backgroundColor = UIColor.yellow.withAlphaComponent(0.4) label.layer.cornerRadius = 8 // Optional: Set corner radius for rounded corners label.layer.masksToBounds = true 

More Tags

jms sha custom-button downsampling digital-ocean refresh field-description transparent dependencies wireshark

More Programming Questions

More Pregnancy Calculators

More Other animals Calculators

More Weather Calculators

More Animal pregnancy Calculators