Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9059743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:03:10+00:00 2026-06-16T15:03:10+00:00

I would like to change the font-size in an HTML string that I have

  • 0

I would like to change the font-size in an HTML string that I have to be half of its size.

E.g.

<div style="font-family:'Arial';font-size:43px;color:#ffffff;">

Will be

<div style="font-family:'Arial';font-size:21.5px;color:#ffffff;">

and

<div style="font-size:12px;">

Will be

<div style="font-size:6px;">

How can I do it with NSRegularExpression?

Please note that 12 and 6 and 43 and 21.5 are only examples. I need regex since it has to be a general solution for different font-size

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:03:12+00:00Added an answer on June 16, 2026 at 3:03 pm

    I am a bit reluctant to give an answer using regular expressions, because it has been stated repeatedly that parsing HTML with regex is considered harmful, impossible, dangerous to your mind, etc. And all that is correct, it is not my intention to claim anything different.

    But even after all that warnings, OP has explicitly asked for a regex solution, so I am going to share this code. It can at least be useful as an example how to modify a string by looping over all matches of a regular expression.

    NSString *htmlString =
        @"<div style=\"font-family:'Arial';font-size:43px;color:#ffffff;\">\n"
        @"<div style=\"font-size:12px;\">\n";
    
    NSRegularExpression *regex;
    regex = [NSRegularExpression regularExpressionWithPattern:@"font-size:([0-9]+)px;"
                                                      options:0
                                                        error:NULL];
    
    NSMutableString *modifiedHtmlString = [htmlString mutableCopy];
    __block int offset = 0;
    [regex enumerateMatchesInString:htmlString
                            options:0
                              range:NSMakeRange(0, [htmlString length])
                         usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                             // range = location of the regex capture group "([0-9]+)" in htmlString:
                             NSRange range = [result rangeAtIndex:1];
                             // Adjust location for modifiedHtmlString:
                             range.location += offset;
                             // Get old point size:
                             NSString *oldPointSize = [modifiedHtmlString substringWithRange:range];
                             // Compute new point size:
                             NSString *newPointSize = [NSString stringWithFormat:@"%.1f", [oldPointSize floatValue]/2];
                             // Replace point size in modifiedHtmlString:
                             [modifiedHtmlString replaceCharactersInRange:range withString:newPointSize];
                             // Update offset:
                             offset += [newPointSize length] - [oldPointSize length];
                         }
     ];
    
    NSLog(@"%@", modifiedHtmlString);
    

    Output:

    <div style="font-family:'Arial';font-size:21.5px;color:#ffffff;">
    <div style="font-size:6.0px;">
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to change the text style/ font size /font colour for my
I have a background image on a div and I would like to change
I would like to change the font size of all texts in Plot and
I have the following bit of HTML: <ul class=unstyled style=font-size:16px;> {% if stuff %}
I have html eg below and would like to use regular expressions to change
I would like to change the font size of several rss links found inside
I have a ListView and each item have a TextView. I would like change
I would like to change the default style for vector features in a map
I would like to change visual studio's default configurations (Debug,Release). That is, change the
I would like to change the text size for some page part, e.g. for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.