Hamdouchi Interactive
  • Welcome
  • Mobile
  • Tech Blog
    • Swift
    • SwiftUI
  • Let's Talk

Unlocking the Power of Swift:
A Tech Blog Series

Welcome to our tech blog series dedicated to exploring the world of Swift and its related topics. Our goal is to provide valuable insights and in-depth analysis on the latest advancements in the Swift programming language, including design patterns and data structures. Stay tuned for our upcoming articles and join the discussion on the exciting world of Swift programming!

Explore all Swift Topics

Enhancing UILabels with Strikethrough Text in Swift

10/14/2023

0 Comments

 
Picture
​In this blog post, we'll explore how to extend the functionality of UILabel in Swift by adding a strikethrough text feature. We'll provide a real-world example of how this can be used and provide code examples to help you implement it in your own iOS applications.

​The Problem

​In many iOS applications, you may need to display text with a strikethrough style to indicate that it's no longer relevant or has been updated. While UILabel is a common choice for displaying text, it doesn't natively support strikethrough text. To address this limitation, we can create an extension for UILabel to add the strikethrough functionality.

​The Solution

​We'll start by creating an extension for UILabel that allows us to easily apply a strikethrough style to the label's text. Here's the Swift code for the extension:

    
​In this code, we define an addStrikethrough method for UILabel. This method takes two parameters: strikethroughStyle (an NSUnderlineStyle) and strikethroughColor (a UIColor). It applies a strikethrough style to the label's text by creating a NSMutableAttributedString, adding the appropriate attributes (strikethrough style and color), and setting the attributed text back to the label.

​Real-World Example

​Now, let's see how we can use this extension in a real-world example. Imagine you have a to-do list app, and you want to display completed tasks with a strikethrough style to visually indicate their completion status. Here's how you can achieve this:

    
In this example, we have a ProductViewController with two UILabels: originalPriceLabel and discountedPriceLabel. The originalPriceLabel is used to display the original price of a product, which we want to indicate as discounted. We apply the strikethrough style and a red color to this label using our addStrikethrough extension method.

The result will be that the original price $200 is displayed with a strikethrough, indicating that it was the original price, while the discounted price $150 is shown as the current discounted price.

​This approach is commonly used in e-commerce apps to highlight discounts and special offers, making it easier for users to see the difference in pricing.

By extending UILabel with the addStrikethrough method, you can easily enhance your iOS applications to display strikethrough text when needed. This feature can be particularly useful for to-do lists, shopping apps, or any application where you want to visually indicate completed or outdated information. Feel free to use and adapt this code to suit your specific needs and create a more user-friendly and informative interface in your iOS apps.
0 Comments
    Picture

    Mohamed Hamdouchi


    Author

    Lead iOS Engineer.
    I help develop Design System Libraries.
    Creative Thinker.
    Featured on the App Store with 4M+ downloads.


    Archives

    November 2023
    October 2023
    August 2023
    July 2023
    June 2023
    May 2023
    April 2023
    March 2023
    February 2023
    January 2023
    December 2022


    Categories

    All
    Abstract Factory
    Animation
    Array
    Associatetype
    Avl
    Behavioral
    Bridge
    Builder
    Case
    Chaining
    Chain Of Responsibility
    Class
    Closure
    Coalescing
    Coercion
    Command
    Composite
    Computed Property
    Conditional Conformance
    Crash
    Creational
    Data Structure
    Decorator
    Default Case
    Design Pattern
    Dictionary
    Enum
    Extension
    Facade
    Factory
    Factory Method
    Flyweight
    Function
    Generics
    Graphs
    Guard
    Hash Table
    Heap
    Initialization
    Interpreter
    Ios
    Iterator
    Linked List
    Lowercase
    Mapping
    Mediator
    Memento
    Observer
    Optional
    Pattern Matching
    Protocol
    Prototype
    Proxy
    Quadratic Probing
    Queue
    Search
    Self
    Sequence
    Singleton
    Stack
    State
    Strategy
    Strikethrough
    Struct
    Structural
    Subscript
    Swift
    Template Method
    Text
    Tree
    Tries
    Try
    UIKit
    Uilabel
    UITableView
    Uppercase
    Visitior

​​COPYRIGHT © 2009 HAMDOUCHI INTERACTIVE, LLC. ​ALL RIGHTS RESERVED
  • Welcome
  • Mobile
  • Tech Blog
    • Swift
    • SwiftUI
  • Let's Talk